package es_deusto_engineering_IS_HanoiTower;

import java.util.ArrayList;
import java.util.List;

import es.deusto.ingenieria.is.search.formulation.State;

public class Environment extends State implements Cloneable{

	/**
	 * @param args
	 */
	
	private Peg origin;
	private Peg destination;
	private List<Peg> pegs;

	
	/**Main constructor of the Environment class.
	 * @param numPegs
	 * @param orig
	 * @param dest
	 * @param numDisks
	 */
	public Environment(int numPegs, int orig, int dest, int numDisks)
	{
		super();
		this.pegs = new ArrayList<Peg>(numPegs);
		for(int i = 0; i < numPegs; i ++)
		{
			pegs.add(new Peg(i));
		}
		//set the origin
		this.origin = pegs.get(orig-1);
		this.destination = pegs.get(dest-1);
		
		for(int j = numDisks; j > 0;j--)
		{
			this.origin.addDisk(new Disk(j));
		}
	}
	
	@Override
	public String toString() {
		return "Environment [origin=" + origin + ", destination=" + destination
				+ ", pegs=" + pegs + "]";
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	}

	public Peg getOrigin() {
		return origin;
	}

	public void setOrigin(Peg origin) {
		this.origin = origin;
	}

	public Peg getDestination() {
		return destination;
	}

	public void setDestination(Peg destination) {
		this.destination = destination;
	}

	public List<Peg> getPegs() {
		return pegs;
	}

	public void setPegs(List<Peg> pegs) {
		this.pegs = pegs;
	}

	//Clone method of Environment
	public Environment clone()
	{
		Environment myCopy = null;
		try {
			myCopy = (Environment) super.clone();
			
			//we create the arrayList before add the different pegs
			myCopy.pegs = new ArrayList<Peg>();
			
			if(!this.pegs.isEmpty())
			{
			//make a loop in order to clone all the pegs of the list of pegs of the environment
			for(int i = 0; i < this.pegs.size(); i++)
			{
				Peg p = (Peg) this.pegs.get(i);
				Peg copy = (Peg) p.clone();
				myCopy.pegs.add(copy);
			}
			
			myCopy.setOrigin(myCopy.pegs.get(this.origin.getPosition()));
			myCopy.setDestination(myCopy.pegs.get(this.destination.getPosition()));
			}
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return myCopy;
	}
	
	@Override
	//We redefine the equals method of Environment
	public boolean equals(Object envi) {
		//We check if the parameter object is of class Environment
		if(this.getClass().equals(envi.getClass()) && envi != null)
		{
			if(this.origin.equals(((Environment) envi).origin)  && this.destination.equals(((Environment) envi).destination) )
			{
				//We check if all the pegs of the peg lists are identical and located in the same position
				int cont = 0;
				if(this.pegs.size() != ((Environment)envi).pegs.size())
					return false;
				else{
				for(int i = 0; i < this.pegs.size(); i ++)
				{
					if(!this.pegs.get(i).equals(((Environment)envi).pegs.get(i)))
					{
						cont++;
					}
				}
				if(cont > 0)
					return false;
				else
					return true;
				}
			}
			return false;		
		}
		else
			return false;
	}
	
}
